const {Model, DataTypes} = require("sequelize");
const {sequelize} = require("../config/mysql_config")

class User extends Model {
    //todo 此处可以定义方法
}
User.init({
        id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true },

        userName: {
        field: 'user_name',
        type: DataTypes.STRING(20),
        allowNull: false,
        defaultValue: '',
        unique: true,
        comment: '用户名'
    },
    dataOfBirth: {
        field: 'data_of_birth',
        type: DataTypes.DATEONLY,
        allowNull: true,
        comment: '出生日期'
    },
    gender: {
        type: DataTypes.TINYINT,
        allowNull: true,
        comment: '性别：0-女，1-男'
    },
    bio: {
        type: DataTypes.STRING(100),
        allowNull: true,
        comment: '个人简介'
    },
    aboutMe: {
        field: 'about_me',
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '自我介绍'
    },
    email: {
        type: DataTypes.STRING(255),
        allowNull: true,
        comment: '邮箱',
    },
    phoneNumber: {
        field: 'phone_number',
        type: DataTypes.STRING(20),
        allowNull: true,
        comment: '电话号码'
    },
    coin: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        defaultValue: 0,
        comment: '当前金币余额'
    },
    password: {
        type: DataTypes.STRING(255),
        allowNull: false,
        defaultValue: '',
        comment: "用户密码"
    }
},
    {sequelize,
        modelName: 'User',
        tableName: 'users',
        timestamps: true
    }
    )


class AdvisorFavorite extends Model {}
AdvisorFavorite.init({
    id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true },

    userId: {
        field: 'user_id',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        comment: '用户ID'
    },
    advisorId: {
        field: 'advisor_id',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        comment: '顾问ID'
    },
    note: {
        type: DataTypes.STRING(100),
        allowNull: true,
        comment: '备注'
    },
    isDeleted: {
        type: DataTypes.TINYINT,
        allow: false,
        defaultValue: 0,
        comment: '是否取消收藏顾问'
    }
}, {
    sequelize,
    modelName: 'AdvisorFavorite',
    tableName: 'advisor_favorites',
    timestamps: true,
    paranoid: true,                 // 启用 deletedAt 软删除
    underscored: true,
    deletedAt: 'deleted_at',
    indexes: [
        { unique: true, fields: ['user_id','advisor_id','deleted_at'], name: 'uniq_user_advisor_active' },

    ],
    comment: '顾问收藏（软删除支持反复收藏）'
});

module.exports = {
    User,
    AdvisorFavorite
}
